/*
* Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
* Description: hccl communication connection manager declaration
* Author:yanzhenghang
* Create: 2023-11-5
*/

#ifndef HCCL_COMM_CONN_MGR_H
#define HCCL_COMM_CONN_MGR_H

#include <unordered_set>
#include <unordered_map>
#include <mutex>
#include "comm.h"
#include "hccl_comm_conn.h"

namespace hccl {

class HcclCommConnMgr {
public:
    static HcclCommConnMgr &GetInstance();

    HcclResult AddAndGetCommConn(HcclCommConn *&commConn);

    HcclResult AddCommConn(HcclCommConn *&commConn);

    HcclResult DelCommConn(HcclCommConn *commConn);
    bool IsExceedMaxLinkNum(u32 role);
    bool IsExistCommConn(HcclAddr &connectAddr);
    void InsertConnectCommMap(HcclAddr &connectAddr, HcclConn &conn);
    void DeleteConnectCommMap(HcclAddr &connectAddr);

private:
    HcclCommConnMgr();
    ~HcclCommConnMgr();
    HcclResult InitRa();
    HcclResult UninitRa();

    static constexpr u32 COMM_CONN_NUM_ONE = 1;
    std::unordered_set<HcclCommConn *> commConnSet_{};
    mutable std::mutex commConnMtx_{};
    s32 defaultDevId_{ 0 };
    bool raInited_{ false };
    ra_init_config raConfig_{ DEFAULT_INIT_PHY_ID, DEFAULT_INIT_NIC_POS, DEFAULT_HDC_TYPE };

    std::mutex connectCommMapMtx_{};
    std::map<uint32_t, HcclConn> connectCommMap_{};
};
}

#endif